package com.zqxq.lottery.controller.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.bo.sys.LotteryHnumBO;
import com.zqxq.lottery.entity.o2o.sys.LotteryHnumO2O;
import com.zqxq.lottery.entity.po.sys.LotteryHnumPO;
import com.zqxq.lottery.entity.vo.sys.LotteryHnumVO;
import com.zqxq.lottery.service.sys.ILotteryHnumService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 说    明：   历史开奖
 * 创 建 人：	一刀·胡
 * 创建时间：	2018/3/23 11:50
 * 修 改 人：
 * 修改日期：
 **/
@RestController
@Slf4j
public class LotteryHnumController extends SysBaseController{

    @Autowired
    private ILotteryHnumService lotteryHnumService;

    @ApiOperation(value="分页条件查询历史开奖", notes="分页条件查询历史开奖")
    @PostMapping("/lotteryhnum/page")
    public Result page(@RequestBody LotteryHnumBO search){
        Result rs = new Result();
        try{
            Page page = lotteryHnumService.queryPage(search);
            rs.setData(page);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("List LotteryHnum Error:",e);
        }
        return rs;
    }

    @ApiOperation(value="获取各彩种最后一期开奖结果信息", notes="获取各彩种最后一期开奖结果信息")
    @PostMapping("/lotteryhnum/last")
    public Result queryLastOpenList(){
        Result rs = new Result();
        try{
            List list = lotteryHnumService.queryLastOpenList();
            rs.setData(list);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("LastOPenList LotteryHnum Error:",e);
        }
        return rs;
    }

    @PostMapping("/lotteryhnum/get")
    public Result get(@RequestParam Long id){
        Result rs = new Result();
        try{
            LotteryHnumPO po = lotteryHnumService.selectById(id);
            LotteryHnumVO vo = new LotteryHnumVO();
            if(null != po)
                vo = LotteryHnumO2O.toVO(po);
            rs.setData(vo);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Get LotteryHnum Error:",e);
        }
        return rs;
    }
    @PostMapping("/lotteryhnum/getLotteryHnumByPeriod")
    public Result getLotteryHnumByPeriod(@RequestParam Long period){
        Result rs = new Result();
        try{
            LotteryHnumPO po = lotteryHnumService.getLotteryHnumByPeriod(period);
            LotteryHnumVO vo = new LotteryHnumVO();
            if(null != po)
                vo = LotteryHnumO2O.toVO(po);
            rs.setData(vo);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Get LotteryHnum Error:",e);
        }
        return rs;
    }

    //添加、修改
    @PostMapping("/lotteryhnum/add")
    public Result add(@RequestBody LotteryHnumVO vo){
        Result rs = new Result();
        try{
            vo = lotteryHnumService.save(vo);
            rs.setData(vo);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Add LotteryHnum Error:",e);
        }
        return rs;
    }

    //从极速平台同步开奖历史数据
    @PostMapping("/lotteryhnum/syncFromJisu")
    public Result syncLotteryHnumFromJisuApi(@RequestParam Long lotteryId,@RequestParam(required = false) String period){
        Result rs = new Result();
        try{
            if(null == lotteryId)
                throw new Exception("参数错误！");
            LotteryHnumVO data = lotteryHnumService.saveDataFromJisuApi(lotteryId,period);
            rs.setData(data);;
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Synchronized LotteryHnumFrom JisuApi Error:",e);
        }
        return rs;
    }

    //生成下期开奖周期
    @PostMapping("/lotteryhnum/newNext")
    public Result newNext(@RequestParam Long lotteryId){
        Result rs = new Result();
        try{
            LotteryHnumVO vo = lotteryHnumService.updateLotteryForClose(lotteryId);
            rs.setData(vo);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("NewNext LotteryHnum Error:",e);
        }
        return rs;
    }

    //关闭当前期，生成下期
    @PostMapping("/lotteryhnum/closeLottery")
    public Result closeLottery(@RequestParam Long lotteryId){
        Result rs = new Result();
        try{
            lotteryHnumService.updateLotteryForClose(lotteryId);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("NewNext LotteryHnum Error:",e);
        }
        return rs;
    }

}
